package site.addzero.repl

import org.koin.core.annotation.Single
import site.addzero.app.AdvancedRepl
import site.addzero.app.ParamDef
import site.addzero.cli.platform.PlatformTools
import kotlin.reflect.typeOf

/**
 * KillPortCommand的REPL包装类，实现AdvancedRepl接口
 */
@Single
class KillPortRepl : AdvancedRepl<KillPortRepl.KillPortCommandParams, Unit> {
    override val command: String = "kill-port"
    override val description: String = "杀掉占用指定端口的进程"
    override val paramDefs: List<ParamDef> = listOf(
        ParamDef(
            name = "port",
            type = typeOf<Int>(),
            description = "要杀掉的端口号"
        )
    )

    data class KillPortCommandParams(
        val port: Int
    )

    override fun eval(params: KillPortCommandParams) {
        val port = params.port
        if (port !in 1..65535) {
            System.err.println("端口号必须在 1-65535 范围内")
            return
        }

        println("正在尝试杀掉占用端口 $port 的进程...")
        val success = PlatformTools.killPort(port)

        if (success) {
            println("成功杀掉占用端口 $port 的进程")
        } else {
            System.err.println("杀掉占用端口 $port 的进程失败")
        }
    }

//    override fun createParams(values: List<Any?>): KillPortCommandParams {
//        return KillPortCommandParams(
//            port = values[0] as Int
//        )
//    }
}
